package com.gkzf.ai.module.crm.service.creditReport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gkzf.ai.framework.mybatis.core.query.QueryWrapperX;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.CreditReportCreditCardDetailsDO;
import com.gkzf.ai.module.crm.dal.mysql.creditReport.CreditReportCreditCardDetailsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Objects;

import static com.gkzf.ai.module.crm.util.AnalysisJsonObjectUtils.convertMoney;

/**
 * CRM 征信报告信用卡明细 Service 实现类
 *
 * @author 国科智飞
 */
@Service
@Validated
public class CreditReportCreditCardDetailsServiceImpl implements CreditReportCreditCardDetailsService {

    @Resource
    private CreditReportCreditCardDetailsMapper creditReportCreditCardDetailsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCreditReportCreditCardDetails(List<CreditReportCreditCardDetailsDO> createReqVO,Long creditReportDetailsId,String creator) {
        createReqVO.forEach(item -> {
            item.setCreditReportDetailsId(creditReportDetailsId);
            item.setCreator(creator);
            item.setUpdater(creator);
        });
        // 插入
        creditReportCreditCardDetailsMapper.insertBatch(createReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCreditReportCreditCardDetails(List<CreditReportCreditCardDetailsDO> updateReqVO) {
        // 更新
        creditReportCreditCardDetailsMapper.updateById(updateReqVO);
    }

    @Override
    public List<CreditReportCreditCardDetailsDO> getCreditReportCreditCardDetailsList(Long creditReportDetailsId) {
        LambdaQueryWrapper<CreditReportCreditCardDetailsDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CreditReportCreditCardDetailsDO::getCreditReportDetailsId, creditReportDetailsId);
        return creditReportCreditCardDetailsMapper.selectList(queryWrapper);
    }

    /**
     * 获取特殊用户的信用卡月还款金额
     *
     * @param creditReportDetailsId 明细编号
     */
    @Override
    public String getMonthlyRepaymentAmount(Long creditReportDetailsId) {
        QueryWrapperX<CreditReportCreditCardDetailsDO> wrapper = new QueryWrapperX<>();
        wrapper.select("SUM( monthly_repayment ) AS monthlyRepayment " ); // 指定需要聚合的列
        wrapper.eq("credit_report_details_id", creditReportDetailsId); // 指定查询条件
        List<CreditReportCreditCardDetailsDO> creditReportCreditCardDetailsDOS = creditReportCreditCardDetailsMapper.selectList(wrapper);
        if(Objects.nonNull(creditReportCreditCardDetailsDOS) && !creditReportCreditCardDetailsDOS.isEmpty()) {
            if (null == creditReportCreditCardDetailsDOS.get(0)){
                return "0";
            }
            return convertMoney(creditReportCreditCardDetailsDOS.get(0).getMonthlyRepayment());
        }
        return "0";
    }

}